package com.codeerror.demo.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.codeerror.demo.R;
import com.codeerror.demo.adapter.WordsBgSelectRecyclerAdapter;
import com.codeerror.demo.adapter.WordsListRecyclerAdapter;
import com.codeerror.demo.bean.WordsBean;
import com.codeerror.demo.bean.WordsBgBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 修改每日写给自己的一句话的弹窗
 */
public class WordsDialog extends Dialog {
    public WordsDialog(@NonNull Context context) {
        super(context);
    }

    public static class Builder {
        private final WordsDialog dialog;
        private final View layout;

        private final ImageView close;
        private EditText et;
        private RecyclerView rvBg;
        private Button btnCreate, btnDelete;

        private int type;
        private WordsBean hasWordsBean;
        private int defaultBgId;
        private long curTime; // 当前时间
        private OnWordsCreateListener onWordsCreateListener;
        private OnWordsDeleteListener onWordsDeleteListener;

        public Builder(Context context) {
            dialog = new WordsDialog(context);
            layout = LayoutInflater.from(context).inflate(R.layout.layout_dialog_words
                    , null, false);
            close = layout.findViewById(R.id.iv_dialog_words_close);
            et = layout.findViewById(R.id.et_dialog_words);
            rvBg = layout.findViewById(R.id.rv_dialog_words_bg_select);
            btnCreate = layout.findViewById(R.id.btn_create_new_words);
            btnDelete = layout.findViewById(R.id.btn_delete_words);
        }

        /**
         * 设置对话框类型
         */
        public Builder setType(int type) {
            this.type = type;
            return this;
        }

        /**
         * 设置已存在的WordsBean
         */
        public Builder setHasWordsBean(WordsBean hasWordsBean) {
            this.hasWordsBean = hasWordsBean;
            return this;
        }

        /**
         * 设置默认背景图片
         */
        public Builder setDefaultBgId(int defaultBgId) {
            this.defaultBgId = defaultBgId;
            return this;
        }

        public Builder setTime(long curTime) {
            this.curTime = curTime;
            return this;
        }

        /**
         * 设置句子创建/修改成功后的监听事件
         */
        public Builder setOnWordsCreateListener(OnWordsCreateListener onWordsCreateListener) {
            this.onWordsCreateListener = onWordsCreateListener;
            return this;
        }

        /**
         * 设置句子删除后的监听事件
         */
        public Builder setOnWordsDeleteListener(OnWordsDeleteListener onWordsDeleteListener) {
            this.onWordsDeleteListener = onWordsDeleteListener;
            return this;
        }

        public WordsDialog create() {
            dialog.setContentView(layout);

            if (type == WordsListRecyclerAdapter.ADD_WORDS){
                // 新建每日一句，进行初始化工作
                et.setHint("在这里写下\n送给自己的话吧");
                et.setBackgroundResource(defaultBgId);
                hasWordsBean = new WordsBean(); // 初始化WordsBean
                hasWordsBean.setBgId(defaultBgId);
            }else if (type == WordsListRecyclerAdapter.NORMAL_WORDS){
                // 编辑每日一句内容，显示可以删除的按钮
                // 设置EditText内容
                et.setText(hasWordsBean.getWords());
                et.setBackgroundResource(hasWordsBean.getBgId());
                btnDelete.setVisibility(View.VISIBLE);
                btnDelete.setOnClickListener(view -> {
                    onWordsDeleteListener.onDeleteWords();
                    // TODO 全局对话框提示确定要删除吗？
                    // TODO 提示删除成功
                    new PinkToast.Builder(dialog.getContext()).setText("删除成功").showToast();
                    dialog.dismiss();
                });
            }
            // 初始化可以选中的句子背景图片
            List<WordsBgBean> bgList = new ArrayList<>();
            initBgList(bgList);
            WordsBgSelectRecyclerAdapter adapter =
                    new WordsBgSelectRecyclerAdapter(bgList, hasWordsBean.getBgId());
            adapter.setOnImageSelectListener(bgId -> {
                // 回调获取已选择的背景图片，并实时设置
                et.setBackgroundResource(bgId);
                hasWordsBean.setBgId(bgId);
            });
            rvBg.setLayoutManager(new StaggeredGridLayoutManager(2,
                    StaggeredGridLayoutManager.HORIZONTAL));
            rvBg.setAdapter(adapter);

            // 创建句子的接口
            btnCreate.setOnClickListener(view -> {
                if (TextUtils.isEmpty(et.getText().toString())){
                    // TODO 提示输入为空
                    new PinkToast.Builder(dialog.getContext()).setText("输入不能为空哦").showToast();
                }else {
                    // 获取句子内容
                    hasWordsBean.setWords(et.getText().toString());
                    hasWordsBean.setTime(curTime);
                    onWordsCreateListener.onCreateWords(hasWordsBean);
                    //TODO 根据type提示创建修改成功
                    dialog.dismiss();
                }
            });
            close.setOnClickListener(view -> dialog.cancel());
            return dialog;
        }

        /**
         * 初始化每日一句背景图片列表数据
         */
        private void initBgList(List<WordsBgBean> bgList) {
            bgList.add(new WordsBgBean(R.drawable.words_bg_pink));
            bgList.add(new WordsBgBean(R.drawable.words_bg_blue));
            bgList.add(new WordsBgBean(R.drawable.words_bg_teal));
            bgList.add(new WordsBgBean(R.drawable.words_bg_light_blue));
            bgList.add(new WordsBgBean(R.drawable.words_bg_light_purple));
        }
    }

    /**
     * 展示Dialog，添加Dialog显示和消失的动画
     */
    public void showDialog() {
        Window window = getWindow();
//        // 清除背景的黑色
//        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        window.setBackgroundDrawableResource(R.color.transparent);
        // 缩放显示和消失
        window.setWindowAnimations(R.style.dialogWindowShowAnim);
        // 可按后退键退出，但不可点外边退出
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        show();
    }

    public interface OnWordsCreateListener {
        /**
         * 创建新的每日一句
         * @param wordsBean 修改后的WordsBean
         */
        void onCreateWords(WordsBean wordsBean);
    }

    public interface OnWordsDeleteListener{
        /**
         * 删除当前所选的每日一句
         */
        void onDeleteWords();
    }
}
